package com.atguigu;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;


@WebServlet("/servlet6")
public class servlet6 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println(username);
        System.out.println(Arrays.toString(hobbies));
        Enumeration<String> parameterNames = req.getParameterNames();
        System.out.println("通过请求名字来获取请求");
        while (parameterNames.hasMoreElements()){
            String s = parameterNames.nextElement();
            String[] parameterValues = req.getParameterValues(s);
            if(parameterValues.length>1){
                System.out.println(s+" = "+ Arrays.toString(parameterValues));
            }else{
                System.out.println(s+" = "+req.getParameter(s));
            }
        }
        System.out.println("map方法遍历所有请求信息");
        Map<String, String[]> parameterMap = req.getParameterMap();
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            String key = stringEntry.getKey();
            String[] value = stringEntry.getValue();
            if(value.length>1){
                System.out.println(key+" = "+Arrays.toString(value));
            }else{
                System.out.println(key+" = "+value[0]);
            }
        }
        System.out.println(req.getServletPath());
        System.out.println(req.getContentLength());
    }
}
